using System.Windows.Forms;
using WindowsLive.Writer.Api;
using Microsoft.Win32;
using System.Linq;
using ScottIsAFool;

namespace WordpressFeaturedImagePlugin
{
    [WriterPlugin("95b4c76e-8760-4e23-a20c-d3994dea66fb", "Wordpress Featured Image Plugin",
        PublisherUrl = "http://dev.scottisafool.co.uk",
        Description = "The Featured Image Plugin is for wordpress.org blogs only (at this time). It will allow you to set the featured image for a post when you publish it.",
        ImagePath = "writer.png",
        HasEditableOptions = true)]

    public class FeaturedImagePlugin : PublishNotificationHook
    {
        public override void Initialize(IProperties pluginOptions)
        {
            base.Initialize(pluginOptions);
        }

        public override void OnPostPublish(IWin32Window dialogOwner, IProperties properties, IPublishingContext publishingContext, bool publish)
        {
            if (!publishingContext.ServiceName.ToLower().Contains("wordpress") && publishingContext.ServiceName.ToLower().Contains("wordpress.com"))
                return;
            PluginSettings settings = new PluginSettings(Options.GetSubProperties(publishingContext.AccountId));
            bool letsDoThis = false;

            // Sees if there is a set range of categories for doing this, if there aren't do it for all posts
            if (!string.IsNullOrEmpty(settings.CheckedCategories))
            {
                // Check each set category and see if the category is in the post
                foreach (var item in WriterUtils.ParseString(settings.CheckedCategories))
                {
                    foreach (var cat in publishingContext.PostInfo.Categories)
                        if (cat.Name == item)
                            letsDoThis = true; // we have a success, don't need to check anymore

                    if (letsDoThis)
                        break;
                }
                if (!letsDoThis) // no set categories exist in the post, don't do anything.
                    return;
            }

            BlogContext context = new BlogContext(publishingContext);
            string username = context.Username;
            string password = settings.Password;
            if (string.IsNullOrEmpty(password))
            {
                using (frmPassword pass = new frmPassword(publishingContext.BlogName))
                {
                    if (pass.ShowDialog() == DialogResult.OK)
                    {
                        if (pass.SavePassword)
                            settings.Password = pass.Password;
                        password = pass.Password;
                    }
                    else
                    {
                        return;
                    }
                }
            }
            OpenFileDialog ofd = new OpenFileDialog()
            {
                Filter = "All Supported Files (*.png, *.gif, *.jpg)|*.jpg;*.png;*.gif",
                Title = "Please choose your image"
            };

            if (ofd.ShowDialog() == DialogResult.OK)
            {
                using (frmPublishing pub = new frmPublishing(username, password, ofd.FileName, context.PostApiUrl, publishingContext.PostInfo.Id, publish, settings.UsePostIdAsImageName))
                {
                    pub.GoBatman();
                    pub.ShowDialog();
                    if (pub.HasErrored)
                    {
                        settings.Password = string.Empty;
                    }
                }
            }
            else
            {
                return;
            }

        }       

        public override void EditOptions(IWin32Window dialogOwner)
        {
            using (frmOptions op = new frmOptions(Options))
            {
                op.ShowDialog();
            }
        }
    }
}
